//#include   "Etc.h"
#include "../includes.h"


void ave_InitValue( ave_ *p)
{
	p->sum_cnt = 0;
	p->index = 0;
	p->sum = 0;
}

ave_ ave_dac = { .sum_cnt=0, .index=0, .sum=0 };

void ave_PushValue( ave_ *p,double v)
{
	double temp;
	int i;
	
	if( p->sum_cnt < DAC_AVE_TIME-1 ){
		p->sum += v;
		p->sum_cnt++; 
		
		if( p->index == 0 ) p->recent = p->sum / p->sum_cnt;
	}
	else{
		temp = p->sum / (double)p->sum_cnt;
		p->ave[ p->index%DAC_AVE_INDEX_SIZE ] = temp;
		if( p->index < 1) p->recent = temp;
		else 
		{
			p->sum=0; // use sum for caculation of average dac
			for( i=0; i< DAC_AVE_INDEX_SIZE && i< p->index; i++) p->sum += p->ave[i];
			p->recent = p->sum/(double)i; // now recent is update..
		}
		p->sum_cnt=0;
		p->sum=0;
		p->index++;
	}
	
}	

